package com.hdoit.kylin.web.group.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.group.model.Project;
import com.hdoit.kylin.logical.group.model.Template;
import com.hdoit.kylin.logical.group.service.IProjectService;
import com.hdoit.kylin.logical.group.service.ITemplateService;
import com.hdoit.kylin.logical.relative.service.IProjectUserService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.TemplateSearch;
import com.hdoit.kylin.logical.viewmodel.project.ProjectSearch;

/**
 * @author luozhicheng
 * @email
 * @createtime 2017年07月27日 15:17:22
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/project/")
public class ProjectController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/project/";
	}

	private IProjectService projectService;
	private ITemplateService templateService;
	private ITeacherService teacherService;
	private IStudentService studentService;
	private IProjectUserService projectUserService;

	public IProjectUserService getProjectUserService() {
		return projectUserService;
	}
	@Resource
	public void setProjectUserService(IProjectUserService projectUserService) {
		this.projectUserService = projectUserService;
	}
	
	public ITeacherService getTeacherService() {
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}
	public IStudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}
	public ITemplateService getTemplateService() {
		return templateService;
	}
	@Resource
	public void setTemplateService(ITemplateService templateService) {
		this.templateService = templateService;
	}

	public IProjectService getProjectService() {
		return projectService;
	}

	@Resource
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	private final String indexViewName = "index";
	private final String indexForSViewName = "indexForS";
	private final String outputViewName = "output";
	private final String detailedViewName = "projectDetailed";
	private final String detailedAllViewName="detailed";
	private final String templateViewName = "templateDetailed";
	private final String saveViewName = "save";
	private final String saveForSViewName = "saveForS";
	private final String deleteViewName = "delete";
	
	@ResponseBody
	@RequestMapping(value = templateViewName,method = {RequestMethod.GET,
			RequestMethod.POST}, produces = "text/plain;charset=utf-8")
	public String template(TemplateSearch search,String templateId,Model model){
		Template template;
		System.out.println(templateId+"00000000000");
		if (StringUtils.isEmpty(templateId)) {
			template=new Template();
		}else{
			template = templateService.queryModel(templateId);
		}
	//	System.out.println("11111111");
		//System.out.println(project.getProjectId()+"2222222");
		//model.addAttribute("template", template);
		System.out.println(template.getComments()+"22222222");
		return template.getComments();
	}

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			ProjectSearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(projectService.queryCountByMark(search,0));
		System.out.println(projectService.queryCount(search)+"9999999");
		List<Project> projects = projectService.queryList(0, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("projects", projects);
		model.addAttribute("projectSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value = indexForSViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String indexForS(Model model, PagerModel pagerModel,
			ProjectSearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(projectService.queryCount(search));
		List<Project> projects = projectService.queryList(1, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("projects", projects);
		model.addAttribute("projectSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexForSViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String projectDetailed(Model model, String key) {
		Project project;
	
		//System.out.println(key+"00000000000");
		if (StringUtils.isEmpty(key)) {
			//System.out.println("111111");
			project=new Project();
		}else{
			//System.out.println("2222222");
			project = projectService.queryModel(key);
			
		}
	//	System.out.println("11111111");
		//System.out.println(project.getProjectId()+"2222222");
		model.addAttribute("project", project);
		return view(detailedViewName);
	}

	
	@RequestMapping(value = detailedAllViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String projectDetailedForS(Model model, String key,String templateId,TemplateSearch templatesearch,ProjectSearch search) {
		Project project = new Project();
		Template template = new Template();
		String tId = null;
		if (!StringUtils.isEmpty(key)) {
			project = projectService.queryModel(key);
			tId=project.getTemplateId();
			model.addAttribute("tId", tId);
		}
		if (!StringUtils.isEmpty(templateId)) {
			template = templateService.queryModel(templateId);
		}
		model.addAttribute("template", template);
		model.addAttribute("project", project);
		model.addAttribute("projectSearch", search);
		model.addAttribute("templatesearch", templatesearch);
		return view(detailedAllViewName);
	}
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, Project project) {
		if (StringUtils.isEmpty(project.getProjectId())) {
			project.setProjectId(CommonUtils.getKeyId());
			project.setCreateDate(DateUtils.getCurrent());
			project.setCreaterId(getLoginModel().getLoginId());
			project.setCreateName(getLoginModel().getLoginName());
			project.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			if(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId())==null){
				project.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			}
			project.setMark(0);
			project.setModelStatus(ModelStatus.add);
		} else {
			
			project.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if (projectService.applyChange(project)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("project", project);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	
	@RequestMapping(value = saveForSViewName, method = { RequestMethod.POST })
	public String saveForS(Model model, Project project) {
		if (StringUtils.isEmpty(project.getProjectId())) {
			project.setProjectId(CommonUtils.getKeyId());
			project.setCreateDate(DateUtils.getCurrent());
			project.setCreaterId(getLoginModel().getLoginId());
			project.setCreateName(getLoginModel().getLoginName());
			project.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			if(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId())==null){
				project.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			}
			project.setMark(1);
			project.setModelStatus(ModelStatus.add);
		} else {
			
			project.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if (projectService.applyChange(project)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		Template template = new Template();
		model.addAttribute("project", project);
		model.addAttribute("template", template);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(indexForSViewName);
	}
	
	
	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (projectService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}